package ownserver;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.DataManager;
import com.sun.sgs.app.ManagedReference;

public class ShipFactory {
	static ServerShip CreateShip(int ownerID, int typeid, int guid, double x, double y)
	{
		// TODO: guid pitäisi oikeasti luoda täällä jotenkin fiksusti 
		double hp = 0;
		switch (typeid) {
		case 1:
			hp = 100;
			break;
		default:
				
		}
		ServerShip ship = new ServerShip(guid, typeid, x, y, ownerID, hp);
		
		ShipFactory.CreateTurrets(ship);
		return ship;
	}
	
	static void CreateTurrets(ServerShip ship)
	{
		DataManager dataMgr = AppContext.getDataManager();
		ManagedReference<ServerShip> shipref = dataMgr.createReference(ship);
		
		switch (ship.getTypeid()) {
		case 1:
			ServerTurret turret1 = ShipFactory.CreateTurret(0, 1, shipref);
			ServerTurret turret2 = ShipFactory.CreateTurret(1, 1, shipref);
			ship.attachTurret(turret1);
			ship.attachTurret(turret2);
			return;
		default:
			return;
		}
	}
	
	static ServerTurret CreateTurret(int index, int shiptypeid, ManagedReference<ServerShip> shipref) { 
		double x=0,y=0;
		double beginAngle=0, endAngle=0;
		double optimal = 0;
		double baseDamage = 0;
		int cycleDuration = 0;
		double tracking = 0, signatureRadius = 0, falloff = 0;
		switch (shiptypeid) {
		case 1:
			optimal = 100;
			cycleDuration = 5000;
			// TODO: korjaa!
			baseDamage = 1;
			tracking = 1;
			signatureRadius = 10;
			falloff = 10;
			switch (index) {
			case 0:
				x = 7.161;
				y = 0.888;
				beginAngle = 30;
				endAngle = 150;
				break;
			case 1:
				x = 7.161;
				y = -0.408;
				beginAngle = 210;
				endAngle = 330;
				break;
			}
			
			break;
		default:
			return null;
		}
		return new ServerTurret(shipref, beginAngle, endAngle, x, y, optimal, cycleDuration, baseDamage, tracking, signatureRadius, falloff);
	}

}
